<template>
	<a-card :bordered="false">
		<a-form ref="searchFormRef" name="advanced_search" :model="searchFormState" class="ant-advanced-search-form">
			<a-row :gutter="24">
				<a-col :span="6">
					<a-form-item label="申请人" name="applicantId">
						<a-select
							v-model:value="searchFormState.applicantId"
							placeholder="请选择申请人"
							:options="applicants"
							:field-names="{ label: 'name', value: 'id', options: 'children' }"
							allow-clear
						/>
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-form-item label="客户" name="customerId">
						<a-select
							v-model:value="searchFormState.customerId"
							placeholder="请选择客户"
							:options="customs"
							:field-names="{ label: 'name', value: 'id', options: 'children' }"
							allow-clear
						/>
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-form-item label="描述" name="description">
						<a-input v-model:value="searchFormState.description" placeholder="请输入描述" />
					</a-form-item>
				</a-col>
				<a-col :span="6" v-show="advanced">
					<a-form-item label="合同号" name="contractNo">
						<a-input v-model:value="searchFormState.contractNo" placeholder="请输入合同号" />
					</a-form-item>
				</a-col>
				<a-col :span="6" v-show="advanced">
					<a-form-item label="开始时间" name="startDate">
						<a-range-picker v-model:value="searchFormState.startDate" value-format="YYYY-MM-DD HH:mm:ss" show-time />
					</a-form-item>
				</a-col>
				<a-col :span="6" v-show="advanced">
					<a-form-item label="结束时间" name="endDate">
						<a-range-picker v-model:value="searchFormState.endDate" value-format="YYYY-MM-DD HH:mm:ss" show-time />
					</a-form-item>
				</a-col>
				<a-col :span="6" v-show="advanced">
					<a-form-item label="MAC地址" name="macAddress">
						<a-input v-model:value="searchFormState.macAddress" placeholder="请输入MAC地址" />
					</a-form-item>
				</a-col>
				<a-col :span="6" v-show="advanced">
					<a-form-item label="生成状态" name="buildState">
						<a-select
							v-model:value="searchFormState.buildState"
							placeholder="请选择生成状态"
							:options="buildStateOptions"
						/>
					</a-form-item>
				</a-col>
				<a-col :span="6" v-show="advanced">
					<a-form-item label="审批状态" name="approvalStatus">
						<a-select
							v-model:value="searchFormState.approvalStatus"
							placeholder="请选择审批状态"
							:options="approvalStatusOptions"
						/>
					</a-form-item>
				</a-col>
				<a-col :span="6" v-show="advanced">
					<a-form-item label="发放状态" name="state">
						<a-select v-model:value="searchFormState.state" placeholder="请选择发放状态" :options="stateOptions" />
					</a-form-item>
				</a-col>
				<a-col :span="6" v-show="advanced">
					<a-form-item label="授权类型" name="type">
						<a-select v-model:value="searchFormState.type" placeholder="请选择授权类型" :options="typeOptions" />
					</a-form-item>
				</a-col>
				<a-col :span="6" v-show="advanced">
					<a-form-item label="版本" name="version">
						<a-select
							v-model:value="searchFormState.version"
							placeholder="请选择版本"
							:options="versionOptions"
							:field-names="{ label: 'name', value: 'id', options: 'children' }"
							allow-clear
						/>
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-button type="primary" @click="tableRef.refresh()">查询</a-button>
					<a-button style="margin: 0 8px" @click="reset">重置</a-button>
					<a @click="toggleAdvanced" style="margin-left: 8px">
						{{ advanced ? '收起' : '展开' }}
						<component :is="advanced ? 'up-outlined' : 'down-outlined'" />
					</a>
				</a-col>
			</a-row>
		</a-form>
		<s-table
			ref="tableRef"
			:columns="columns"
			:data="loadData"
			:alert="options.alert.show"
			bordered
			:row-key="(record) => record.id"
			:tool-config="toolConfig"
			:row-selection="options.rowSelection"
		>
			<template #operator class="table-operator">
				<a-space>
					<a-button type="primary" @click="formRef.onOpen()" v-if="hasPerm('licenseApplicationAdd')">
						<template #icon><plus-outlined /></template>
						新增
					</a-button>
					<a-button @click="clone" v-if="hasPerm('licenseApplicationAdd')" :disabled="selectedRowKeys.length !== 1">
						<template #icon><plus-outlined /></template>
						复制
					</a-button>
					<xn-batch-button
						v-if="hasPerm('licenseApplicationBatchDelete')"
						buttonName="批量删除"
						icon="DeleteOutlined"
						buttonDanger
						:selectedRowKeys="selectedRowKeys"
						@batchCallBack="deleteBatchLicenseApplication"
					/>
				</a-space>
			</template>
			<template #bodyCell="{ column, record }">
				<template v-if="column.dataIndex === 'buildState'">
					{{ $TOOL.dictTypeData('BUILD_STATE', record.buildState) }}
				</template>
				<template v-if="column.dataIndex === 'approvalStatus'">
					{{ $TOOL.dictTypeData('APPROVAL_STATUS', record.approvalStatus) }}
				</template>
				<template v-if="column.dataIndex === 'state'">
					{{ $TOOL.dictTypeData('LICENSE_STATUS', record.state) }}
				</template>
				<template v-if="column.dataIndex === 'type'">
					{{ $TOOL.dictTypeData('LICENSE_TYPE', record.type) }}
				</template>
				<template v-if="column.dataIndex === 'action'">
					<a-space>
						<a @click="formRef.onOpen(record)" v-if="hasPerm('licenseApplicationEdit') || (record.approvalStatus != 1 && record.applicantId == userInfo.id)"
							>编辑</a
						>
						<a @click="form2Ref.onOpen(record)">使用状态</a>
						<a @click="formViewRef.onOpen(record)">查看</a>
						<a @click="formApproveRef.onOpen(record)" v-if="hasPerm('licenseApplicationEdit')">审批</a>
						<a @click="create(record)" v-if="hasPerm('licenseApplicationCreate') && record.approvalStatus == 1">生成</a>
						<a :href="record.downloadPath" target="_blank" v-if="record.downloadPath">下载</a>
						<a-popconfirm title="确定要删除吗？" @confirm="deleteLicenseApplication(record)">
							<a-button type="link" danger size="small" v-if="hasPerm('licenseApplicationDelete')">删除</a-button>
						</a-popconfirm>
					</a-space>
				</template>
			</template>
		</s-table>
	</a-card>
	<Form ref="formRef" @successful="tableRef.refresh()" />
	<Form2 ref="form2Ref" @successful="tableRef.refresh()" />
	<FormView ref="formViewRef" @successful="tableRef.refresh()" />
	<FormApprove ref="formApproveRef" @successful="tableRef.refresh()" />
</template>

<script setup name="licenseapplication">
	import tool from '@/utils/tool'
	import { cloneDeep } from 'lodash-es'
	import Form from './form.vue'
	import Form2 from './form2.vue'
	import FormView from './view.vue'
	import FormApprove from './approve.vue'
	import licenseApplicationApi from '@/api/biz/licenseApplicationApi'

	import { message } from 'ant-design-vue'
	import bizGroupApi from "@/api/biz/bizGroupApi";
	import customerInfoApi from "@/api/biz/customerInfoApi";
	import softwareVersionApi from "@/api/biz/softwareVersionApi";
	const searchFormState = ref({})
	const searchFormRef = ref()
	const tableRef = ref()
	const formRef = ref()
	const form2Ref = ref()
	const formViewRef = ref()
	const formApproveRef = ref()
	const toolConfig = { refresh: true, height: true, columnSetting: true, striped: false }
	// 查询区域显示更多控制
	const advanced = ref(false)
	const toggleAdvanced = () => {
		advanced.value = !advanced.value
	}
	const columns = [
		{
			title: '申请人',
			dataIndex: 'applicantName'
		},
		{
			title: '客户',
			dataIndex: 'customerName'
		},
		{
			title: '版本',
			dataIndex: 'versionName'
		},
		// {
		// 	title: '描述',
		// 	dataIndex: 'description'
		// },
		{
			title: '合同号',
			dataIndex: 'contractNo',
			ellipsis: true,
		},
		{
			title: '开始时间',
			dataIndex: 'startDate'
		},
		{
			title: '结束时间',
			dataIndex: 'endDate'
		},
		// {
		// 	title: '生成时间',
		// 	dataIndex: 'createDate'
		// },
		{
			title: '用户',
			dataIndex: 'maxUser',
			width: '50px'
		},
		{
			title: 'MAC地址',
			dataIndex: 'macAddress',
			ellipsis: true,
			width: '150px'
		},
		// {
		// 	title: '申请原由',
		// 	dataIndex: 'reason'
		// },
		// {
		// 	title: '文件附件ID',
		// 	dataIndex: 'fileId'
		// },
		{
			title: '审批状态',
			dataIndex: 'approvalStatus',
			width: '80px'
		},
		{
			title: '生成状态',
			dataIndex: 'buildState',
			width: '80px'
		},
		{
			title: '使用状态',
			dataIndex: 'state',
			width: '80px'
		},
		{
			title: '授权类型',
			dataIndex: 'type'
		} //,
		// {
		// 	title: '扩展信息',
		// 	dataIndex: 'extJson'
		// },
	]
	// 操作栏通过权限判断是否显示
	if (hasPerm(['licenseApplicationEdit', 'licenseApplicationDelete'])) {
		columns.push({
			title: '操作',
			dataIndex: 'action',
			align: 'center',
			width: 350
		})
	}
	const selectedRowKeys = ref([])
	// 列表选择配置
	const options = {
		// columns数字类型字段加入 needTotal: true 可以勾选自动算账
		alert: {
			show: true,
			clear: () => {
				selectedRowKeys.value = ref([])
			}
		},
		rowSelection: {
			onChange: (selectedRowKey, selectedRows) => {
				selectedRowKeys.value = selectedRowKey
			}
		}
	}
	const loadData = (parameter) => {
		const searchFormParam = cloneDeep(searchFormState.value)
		// startDate范围查询条件重载
		if (searchFormParam.startDate) {
			searchFormParam.startStartDate = searchFormParam.startDate[0]
			searchFormParam.endStartDate = searchFormParam.startDate[1]
			delete searchFormParam.startDate
		}
		// endDate范围查询条件重载
		if (searchFormParam.endDate) {
			searchFormParam.startEndDate = searchFormParam.endDate[0]
			searchFormParam.endEndDate = searchFormParam.endDate[1]
			delete searchFormParam.endDate
		}
		return licenseApplicationApi.licenseApplicationPage(Object.assign(parameter, searchFormParam)).then((data) => {
			return data
		})
	}
	// 重置
	const reset = () => {
		searchFormRef.value.resetFields()
		tableRef.value.refresh(true)
	}
	// 删除
	const deleteLicenseApplication = (record) => {
		let params = [
			{
				id: record.id
			}
		]
		licenseApplicationApi.licenseApplicationDelete(params).then(() => {
			tableRef.value.refresh(true)
		})
	}
	const create = (record) => {
		let params = {
			id: record.id
		}
		licenseApplicationApi.licenseApplicationCreate(params).then((res) => {
			if (res === '0') {
				message.success('生成成功')
			}
			tableRef.value.refresh(true)
		})
	}
	const approve = (record) => {
		let params = {
			id: record.id
		}
		licenseApplicationApi.licenseApplicationApprove(params).then((res) => {
			tableRef.value.refresh(true)
		})
	}
	// 批量删除
	const deleteBatchLicenseApplication = (params) => {
		licenseApplicationApi.licenseApplicationDelete(params).then(() => {
			tableRef.value.clearRefreshSelected()
		})
	}
	const buildStateOptions = tool.dictList('BUILD_STATE')
	const approvalStatusOptions = tool.dictList('APPROVAL_STATUS')
	const stateOptions = tool.dictList('LICENSE_STATUS')
	const typeOptions = tool.dictList('LICENSE_TYPE')

	const customs = ref([])
	const applicants = ref([])
	const versionOptions = ref([])
	const userInfo = tool.data.get('USER_INFO')

	const clone = () => {
		let data = options.rowSelection.selectedRows[0]
		let params = {
			applicantId: data.applicantId,
			customerId: data.customerId,
			description: data.description,
			type: data.type,
			version: data.version,
			contractNo: data.contractNo,
			startDate: data.startDate,
			endDate: data.endDate,
			maxUser: data.maxUser,
			macAddress: data.macAddress,
			module: data.module,
			reason: data.reason,
			machineInfo: data.machineInfo
		}
		formRef.value.onOpen(params)
	}
	onMounted(()=>{

		bizGroupApi.bizGroupOwnUserEntity({ id: '1930923922069581826' }).then((res) => {
			applicants.value = res
		})
		customerInfoApi.customerInfoList().then((res) => {
			customs.value = res
		})

		softwareVersionApi.softwareVersionList().then((res) => {
			versionOptions.value = res
		})
	})
</script>
